﻿Imports TBCC.UtilityMobile
Public Class TestForm

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        If Me.optByte.Checked Then
            Dim b() As Byte
            b = COMHelper.Instance.Send4Byte(Convert.ToByte(Me.nudAddress.Value), Convert.ToByte(Me.nudFunction.Value), Convert.ToUInt16(Me.nudOffset.Value), Convert.ToUInt16(Me.nudData.Value))

            If b Is Nothing Then
                'msgbox ""
                addmsg("超时!")
            Else
                Dim str As String = ""
                For i As Integer = 0 To b.Length - 1
                    'str &= String.Format("{0:X2},", b(i))
                    str &= String.Format("{0},", b(i))
                Next

                addmsg(str)

                'MsgBox(System.Text.Encoding.Default.GetString(b))
            End If


        ElseIf Me.optShort.Checked Then
            Dim s() As Short
            s = COMHelper.Instance.Send4Short(Convert.ToByte(Me.nudAddress.Value), Convert.ToByte(Me.nudFunction.Value), Convert.ToUInt16(Me.nudOffset.Value), Convert.ToUInt16(Me.nudData.Value))

            If s Is Nothing Then
                addmsg("超时!")
            Else
                Dim str As String = ""
                For i As Integer = 0 To s.Length - 1
                    str &= String.Format("{0},", s(i))
                Next
                addmsg(str)
            End If

        ElseIf Me.optSingle.Checked Then
            Dim ss() As Single
            ss = COMHelper.Instance.Send4Single(Convert.ToByte(Me.nudAddress.Value), Convert.ToByte(Me.nudFunction.Value), Convert.ToUInt16(Me.nudOffset.Value), Convert.ToUInt16(Me.nudData.Value))

            If ss Is Nothing Then
                addmsg("超时!")
            Else
                Dim str As String = ""
                For i As Integer = 0 To ss.Length - 1
                    str &= String.Format("{0},", ss(i).ToString())
                Next
                addmsg(str)
            End If

        End If
    End Sub

    Dim _i As Integer = 0

    Private Sub addmsg(ByVal str As String)
        _i += 1

        'Debug.WriteLine(Now.ToString("HH:mm:ss.") & Now.Millisecond & " #" & _i & "  ")
        'Exit Sub


        If Me.txtLog.Text.Trim <> "0" Then
            Me.txtLog.Text &= str & ControlChars.NewLine
        Else
            Me.txtLog.Text = ControlChars.NewLine & Now.ToString("HH:mm:ss.") & Now.Millisecond & " #" & _i & "  " & str & ControlChars.NewLine & ControlChars.NewLine & Me.txtLog.Text
        End If

    End Sub

    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
        Dim result As Boolean

        If Me.optByte.Checked Then
            Dim bValue As Byte = Me.nudData.Value
            result = COMHelper.Instance.Write(Convert.ToByte(Me.nudAddress.Value), Convert.ToByte(Me.nudFunction.Value), Convert.ToUInt16(Me.nudOffset.Value), bValue)
        ElseIf Me.optShort.Checked Then
            Dim shortValue As Short = Me.nudData.Value
            result = COMHelper.Instance.Write(Convert.ToByte(Me.nudAddress.Value), Convert.ToByte(Me.nudFunction.Value), Convert.ToUInt16(Me.nudOffset.Value), shortValue)
        ElseIf Me.optSingle.Checked Then
            Dim sngValue As Single = Me.nudData.Value
            result = COMHelper.Instance.Write(Convert.ToByte(Me.nudAddress.Value), Convert.ToByte(Me.nudFunction.Value), Convert.ToUInt16(Me.nudOffset.Value), sngValue)
        End If


        If result Then
            msg.Show("写成功")
        Else
            msg.Show("写失败")
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.txtLog.Text = ""
    End Sub

    Private Sub TestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer = 0
    End Sub
End Class